<template>
  <div style="padding: 15px">
    <vxe-toolbar
      id="inventoryReport"
      :custom="{ storage: true }"
      export
      :refresh="{ query: findList }"
    >
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
        <el-button @click="AllDataEventAll">导出全部</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      keep-source
      highlight-current-row
      :export-config='{type:"csv"}'
      :align="allAlign"
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :loading="loading"
      :data="tableData"
      :edit-config="{ trigger: 'click', mode: 'row', showStatus: true }"
      sort-config.remote
      @sort-change="sortList"
      :seq-config="{startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize}"
    >
      <vxe-table-column type="seq" width="60"></vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="shipmentName"
          title="入驻商名称"
          min-width="200"
        />
        <template #header>
          <el-select v-model="listQuery.shipmentCode" placeholder="请选择入驻商" style="width:100%" clearable @change="changeWarehouse">
            <el-option
              v-for="item in shipmentList"
              :key="item.partyCode"
              :label="item.userName"
              :value="item.partyCode"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodCode"
          title="商品编码"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.goodCode"
            placeholder="商品编码"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodsBarCode"
          title="商品条码"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.goodsBarCode"
            placeholder="商品条码"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodsName"
          title="商品名称"
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.goodsName"
            placeholder="商品名称"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtManufacture"
          title="生产日期"
          min-width="320"
          sortable
          show-header-overflow
          show-overflow
        />
        <template v-slot:header="{ column }">
          <el-date-picker
            v-model="gmtManufactureRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="(value) => changeDate(value, 'gmtManufacture')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtExpire"
          title="过期日期"
          min-width="320"
          sortable
          show-header-overflow
          show-overflow
        />
        <template v-slot:header="{ column }">
          <el-date-picker
            v-model="gmtExpireRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="(value) => changeDate(value, 'gmtExpire')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="warehouseName"
          title="仓库名称"
          min-width="200"
        />
        <template #header>
          <el-select v-model="listQuery.warehouseCode" placeholder="请选择" style="width:100%" clearable @change="changeWarehouse">
            <el-option
              v-for="item in warehouseData"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="warehouseSpaceName"
          title="库区名称"
          min-width="200"
        />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.warehouseSpaceCode" placeholder="请选择" style="width:100%" clearable>
            <el-option
              v-for="item in spaceData"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="locationName"
          title="库位"
          min-width="200"
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.locationName"
            placeholder="库位"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="quantity"
          title="库存数量"
          min-width="180"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.quantityFloor"
              placeholder="下限"
              type='number' style="width:45%;"
              ref="quantity"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.quantityCeiling"
              placeholder="上限"
              type='number' style="width:45%;"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="unitName"
          title="基本单位"
          min-width="100"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.unitName"
            placeholder="基本单位"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="canUseQuantity"
          title="可用数量"
          min-width="180"
          :formatter="formatterCanUseQuantity"
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.canUseQuantityFloor"
              placeholder="下限"
              type='number' style="width:45%;"
              ref="canUseQuantity"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.canUseQuantityCeiling"
              placeholder="上限"
              type='number' style="width:45%;"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="useQuantity"
          title="发货库位数量"
          min-width="180"
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.useQuantityFloor"
              placeholder="下限"
              type='number' style="width:45%;"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.useQuantityCeiling"
              placeholder="上限"
              type='number' style="width:45%;"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodsStatusName"
          title="库存状态"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.goodsStatusName"
            placeholder="库存状态"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="isLock"
          title="锁定状态"
          min-width="150"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-select
            v-model="listQuery.isLock"
            placeholder="锁定状态"
            clearable
          >
            <el-option
              v-for="item in lockList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="spec"
          title="规格"
          min-width="100"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.spec"
            placeholder="规格"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="length"
          title="长(cm)"
          min-width="180"
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.lengthFloor"
              placeholder="下限"
              type='number' style="width:45%;"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.lengthCeiling"
              placeholder="上限"
              type='number' style="width:45%;"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="width"
          title="宽(cm)"
          min-width="180"
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.widthFloor"
              placeholder="下限"
              type='number' style="width:45%;"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.widthCeiling"
              placeholder="上限"
              type='number' style="width:45%;"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="height"
          title="高(cm)"
          min-width="180"
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.heightFloor"
              placeholder="下限"
              type='number' style="width:45%;"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.heightCeiling"
              placeholder="上限"
              type='number' style="width:45%;"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="volume"
          title="体积(cm³)"
          min-width="180"
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.volumeFloor"
              placeholder="下限"
              type='number' style="width:45%;"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.volumeCeiling"
              placeholder="上限"
              type='number' style="width:45%;"
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="weight"
          title="重量(kg)"
          min-width="180"
        />
        <template v-slot:header="{ column }">
          <el-row type="flex" align="middle">
            <el-input
              v-model="listQuery.weightFloor"
              placeholder="下限"
              type='number' style="width:45%;"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.weightCeiling"
              placeholder="上限"
              type='number' style="width:45%;"
            />
          </el-row>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import { getShipments, getchooseAllData, getWarehouse } from '@/api/basicInfo'
import { importInventoryList } from '@/api/comprehensive'
import { exportTask } from '@/api/basicInfo'
export default {
  name: "InventoryReport",
  components: {},
  data() {
    return {
      loading: false,
      allAlign: "center",
      tableData: [],
      statusList:[                       //状态
        { code: "0", name: "待复核" },
        { code: "1",name: "复核中" },
        { code: "2",name: "待出库" },
        { code: "3",name: "出库完成" },
        { code: "4",name: "部分出库" },
        { code: "5",name: "取消出库" },
      ],
      // 锁定状态
      lockList: [
        { code: "0", name: "不锁定" },
        { code: "1", name: "锁定" },
      ],
      // 日期选项
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      gmtManufactureRange: "",
      gmtExpireRange: "",
      spaceData: [],               //库区列表
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
        warehouseSpaceCode: ''
      },
      shipmentList: [],
    };
  },
  computed: {
    warehouseData() {
      return this.$store.getters.warehouse_list
    }
  },
  created() {
    this.findList();

    getShipments().then(res => this.shipmentList =res.data)
  },
  methods: {
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    formatterCanUseQuantity({ cellValue, row }) {
      return row.quantity - row.useQuantity;
    },
    //  获取列表
    findList() {
      this.loading = true;
      return new Promise(reslove =>{
        importInventoryList(this.listQuery).then((res) => {
          this.tableData = res.data.records
          this.listQuery.total = res.data.total
          this.loading = false;
          reslove();
        });
     })
    },
    // 根据仓库变化查询库区数据
    changeWarehouse(code) {
      this.spaceData = []
      this.listQuery.warehouseSpaceCode = ''
      if (code) {
        getchooseAllData(code).then(res => {
          this.spaceData = res.data
        })
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    },
    // 导出全部
    AllDataEventAll () {
      let params = {
        dataType: 'INVENTORS',
        filter: this.listQuery
      }
      exportTask(params).then(data => {
        this.$message.success(data.msg)
      })
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    }
  },
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
